package com.qiguliuxing.dts.wx.web;

import com.alibaba.fastjson.JSON;
import com.qiguliuxing.dts.wx.domain.WxCallbackRecord;
import com.qiguliuxing.dts.wx.service.WxOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/wx/callback")
@Validated
public class WxCallbackController {
	private static final Logger logger = LoggerFactory.getLogger(WxCallbackController.class);

	private final String CALLBACK_PWD="wxcallback321";

	@Autowired
	private WxOrderService wxOrderService;

	@PostMapping("/payCallback")
	public Object payCallback(@RequestBody WxCallbackRecord record, @RequestHeader("callbackPwd") String callbackPwd) throws Exception {
		logger.info("【请求开始】微信支付callback, body:{} ", record);
		if(!CALLBACK_PWD.equals(callbackPwd)){
			logger.error("微信支付callback验证错误");
			return "fail";
		}
		Map attachMap = JSON.parseObject(record.getAttach(), Map.class);
		String module = (String)attachMap.get("module");
		String type = (String)attachMap.get("type");
		if("SHOP".equals(module)
				&& "buy".equals(type)){
			wxOrderService.payNotifyV2(record);

		}
		logger.info("【请求结束】微信支付callback");
		return "ok";
	}

}